﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AcsCommon;
using YiShi.Class;
using Newtonsoft.Json;

namespace YiShi.Exam
{
    public partial class ExamEdit : System.Web.UI.Page
    {
        DbAccess db;
        protected string id;
        protected string name;
        protected string returnActionType;
        protected string buttonHtml;
        public ExamEdit()
        {
            db = DbAccess.Get("Admin");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            object jsonobj = null;
            try
            {
                string action = Request["ActionType"] == null ? "" : Request["ActionType"];
                switch (action.ToLower())
                {
                    case "dataupdate":
                    case "datadelete":
                        DataUpdate();
                        break;
                    case "insert":
                    case "update":
                        jsonobj = Update();
                        break;
                    case "delete":
                        jsonobj = Delete();
                        break;
                    case "datainsert":
                    default:
                        DataInsert();
                        break;
                }
            }
            catch (Exception e1)
            {
                jsonobj = new
                {
                    IsSuccess = "false",
                    Message = e1.Message,
                    Source = e1.Source,
                    StackTrace = e1.StackTrace
                };
            }
            finally
            {
                if (jsonobj != null)
                {
                    Response.Write(JsonConvert.SerializeObject(jsonobj));
                    Response.End();
                }
            }
        }

        private object Delete()
        {
            YiShi.Class.Exam data = new YiShi.Class.Exam(db);
            data.FillFromDb(Request["Id"]);
            data.Delete();
            return new
            {
                IsSuccess = "true"
            };
        }
        private Object Update()
        {
            YiShi.Class.Exam data = new YiShi.Class.Exam(db);
            data.Fill(Request);
            data.Update();
            return new
            {
                IsSuccess = "true"
            };
        }

        private void DataUpdate()
        {
            switch (Request["ActionType"].ToLower())
            {
                case "dataupdate":
                    returnActionType = "Update";
                    buttonHtml = "<button id='UpdateButton'>修改</button>";
                    break;
                case "datadelete":
                    returnActionType = "Delete";
                    buttonHtml = "<button id='UpdateButton'>删除</button>";
                    break;
            }
            id = this.Request["Id"];
            Class.Exam data = new YiShi.Class.Exam(db);
            data.FillFromDb(id);
            name = data.Name;
        }

        private void DataInsert()
        {
            returnActionType = "Insert";
            buttonHtml = "<button id='UpdateButton'>新增</button>";
        }
    }
}
